package com.example.demo.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.HashMap;
//每个访问接口处理业务前后固定要做的事
@ControllerAdvice
public class MyResponseBodyAdvice implements ResponseBodyAdvice {

    @Autowired
    private ObjectMapper objectMapper;
///重写是否有效
    @Override
    public boolean supports(MethodParameter methodParameter, Class aClass) {
        return true;
    }

    @SneakyThrows
    @Override
    public Object beforeBodyWrite(Object o,
                                  MethodParameter methodParameter,
                                  MediaType mediaType,
                                  Class aClass,
                                  ServerHttpRequest serverHttpRequest,
                                  ServerHttpResponse serverHttpResponse) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("status", 0);
        map.put("data", o);
        map.put("msg", "");
// if内的原因：https://blog.csdn.net/wen1652166675/article/details/118367098
//        https://blog.csdn.net/weixin_45176654/article/details/109689869
        if (o instanceof String) {
            // 当前方法要给前端返回一个 json 类型的字符串
            serverHttpResponse.getHeaders().setContentType(
                    MediaType.APPLICATION_JSON);
            // 返回一个 json 字符串
            return objectMapper.writeValueAsString(map);
        }
        return map;
    }
}